/*
 * This file is part of Annuaire.
 *
 * Annuaire is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Annuaire is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Annuaire.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * retourne les structures sous cette forem :
 * [
              [{v:'Mike', f:'Mike<div style="color:red; font-style:italic">President</div>'}, '', 'The President'],
              [{v:'Jim', f:'Jim<div style="color:red; font-style:italic">Vice President</div>'}, 'Mike', 'VP'],
              ['Alice', 'Mike', ''],
              ['Bob', 'Jim', 'Bob Sponge'],
              ['Carol', 'Bob', '']
            ]
 *
 *
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.miage.annuaire.bean;

import org.miage.annuaire.data.*;
import java.util.List;
//import java.lang.StringBuffer;
import org.miage.annuaire.Structure;

/**
 *
 * @author comeulie
 */
public class StructuresBean {

    /**
     * Ajoute un structure et fait le recherche de ses fils
     * @param query
     */
    private void addStructure(StringBuilder query, List<Structure> list, boolean first) {
        for( Structure struct:list ) {

        	boolean bFirst = first;
            if ( first == true )    bFirst = false;
            else                    query.append(",");

            // formate la sortie comme tel :
            //      [{v:'Mike', f:'Mike<div style="color:red; font-style:italic">President</div>'}, '', 'The President']
            query.append("[{v:'");
            query.append(struct.getShortName());
            query.append("', f:'");
            query.append(struct.getShortName());
            query.append("<div><input type=\"checkbox\" checked=true id=\"");
            query.append(struct.getKey());
            query.append("\"/></div>'}, '");
            if(struct.getFather() != null)
                query.append( struct.getFather().getShortName() );
            query.append("','");
            query.append(struct.getCompleteName());
            query.append("']");

            // Ajout des structures Fils
            this.addStructure(query, struct.getChilds(), bFirst);
        }
    }

    /**
     * Retourne la liste de toutes les structur au format javascript Object,
     * pour le plugin AJAX google
     * @return la query a envoyer au plugin google
     */
    public String getStructures() {
        StringBuilder response = new StringBuilder();
        ObjectDataAccess data = DataAccessFactory.getInstance().getObjectDataAccess();  // sus bool
        List<Structure> list = data.getAllStructures();

        this.addStructure(response, list, true);

        //System.out.println(list.size() + " " + list.toString() + " resp:" + response.length() ) ;
        
        /* Return pour le debug ---------------------------------
         return " [  [{v:'Mike',  f:'Mike<div><input type=\"checkbox\" checked=true id=\"mike\" /></div>'}, '', 'The President'], " +
               "    [{v:'Jim',   f:'Jim<div><input type=\"checkbox\" checked=true id=\"jim\" /></div>'}, 'Mike', 'VP'], " +
               "    [{v:'Alice', f:'Alice<div><input type=\"checkbox\" checked=true id=\"alice\" /></div>'}, 'Mike', ''],  "+
               "    [{v:'Bob',   f:'Bob<div><input type=\"checkbox\" checked=true id=\"bob\" /></div>'}, 'Jim', 'Bob Sponge'],  "+
               "    [{v:'Carol', f:'Carol<div><input type=\"checkbox\" checked=true id=\"carol\" /></div>'}, 'Bob', '']  "+
            "]" ;
         --------------------------------------------------------   */
         
        return "[" + response.toString() + "]";
    }
}

/*
[[{v:'Univ', f:'Univ<div><input type="checkbox" checked=true id="1"/></div>'}, '','Universite Paris Ouest Nanterre La Defense']
[{v:'DSP', f:'DSP<div><input type="checkbox" checked=true id="2"/></div>'}, 'Univ','UFR DROIT et SCIENCE POLITIQUE Enseignement']
[{v:'Gestion', f:'Gestion<div><input type="checkbox" checked=true id="3"/></div>'}, 'DSP','UFR D.S.P.  Pole Administratin et Gestion']
,[{v:'Scolarite', f:'Scolarite<div><input type="checkbox" checked=true id="4"/></div>'}, 'DSP','UFR D.S.P.  Pole ScolaritÃ©']
,[{v:'CEDCACE', f:'CEDCACE<div><input type="checkbox" checked=true id="5"/></div>'}, 'DSP','UFR D.S.P.  EA 3457 Centre du droit civil des affaires et du contentieux economique (CEDCACE)']
,[{v:'CREDOF', f:'CREDOF<div><input type="checkbox" checked=true id="6"/></div>'}, 'DSP','UFR D.S.P.  EA 3933 Centre de Recherches et d Etudes sur les Droits Fondamentaux (CREDOF)']
,[{v:'CDPC', f:'CDPC<div><input type="checkbox" checked=true id="7"/></div>'}, 'DSP','UFR D.S.P.  EA 3982 Centre de droit penal et de criminologie (CDPC)']
,[{v:'CRDP', f:'CRDP<div><input type="checkbox" checked=true id="8"/></div>'}, 'DSP','UFR D.S.P.  EA 381 Centre de recherche sur le droit public (CRDP)']
,[{v:'IPAG', f:'IPAG<div><input type="checkbox" checked=true id="9"/></div>'}, 'Univ','Institut de Preparation Ã  l Administration Generale']
        [{v:'', f:'<div><input type="checkbox" checked=true id="10"/></div>'}, 'IPAG','IPAG - Administration']]
 * 
 */